package com.controller;

import com.entity.Grade;
import com.entity.Student;
import com.service.GradeService;
import com.service.StudentService;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/*
控制层
1.添加注解@Controller
2.添加service属性,    @Autowired注解修饰
3.业务逻辑
controller --> service ---> mapper
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private GradeService gradeService;


    /*
    查询所有
     */
    @RequestMapping("/studentList")
    public String findStudentAll(Model model){

        //List<Student> studentList = studentService.findStudentAll();
       List<Student> studentList = studentService.findAllWithGrade();

        model.addAttribute("sList",studentList);
       // System.out.println(studentList);

        return "pages/studentList";
    }

    /*
    查询姓名
     */
    @RequestMapping("/studentname")
    public String findStudentName(Model model){

        List<Student> stname = studentService.findName("");
        model.addAttribute("sname",stname);
        System.out.println(stname);

        return "pages/studentList";
    }


    /*
    查询ID值
     */
//    @RequestMapping("/studentID")
//    public Student findStudentById(Model model){
//
//       Student stID= studentService.findById(1);
//        model.addAttribute("sID",stID);
//        System.out.println(stID);
//
//        //return pages/studentList;
//
//    }


    /*
    添加信息后保存学生信息
     */
    @RequestMapping(value = "/saveStudent",method = RequestMethod.POST)
    public  String saveStudent(Student student){
        studentService.saveStudent(student);
        return "redirect:/studentList";
    }

    @RequestMapping("/addStudent")
    public  String addStudent(Model model){
        //因为添加学生信息需要连接gid外键查询所有grade信息
        List<Grade> list = gradeService.findAll();
        model.addAttribute("gradeList",list);
        System.out.println(list);
        return "pages/StudentAdd";
    }



}
